#include "mainwindow.h"

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <qdatetime.h>
#include "tool.h"
#include "logger.h"

void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    // 将日志消息输出到控制台和日志文件
    QFile outFile(QApplication::applicationDirPath() + "/app_log.txt");
    qDebug()<<outFile.fileName();
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss:zzz");
    QString function = context.function;
    QString prefix = "["+currentTime+"]-";
    if(getSetting("logLevel").toInt()==1){
        prefix = prefix + function + "-";
    }
    switch (type) {
    case QtDebugMsg:
        ts << prefix << "Debug/: " << msg << endl;
        break;
    case QtInfoMsg:
        ts << prefix << "Info/: " << msg << endl;
        break;
    case QtWarningMsg:
        ts << prefix << "Warning/: " << msg << endl;
        break;
    case QtCriticalMsg:
        ts << prefix << "Critical: " << msg <<endl;
        break;
    case QtFatalMsg:
        ts << prefix << "Fatal/: " << msg << endl;
        abort();
    }
    outFile.close();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    qInstallMessageHandler(myMessageHandler);
    Logger::initialize(ROOT_PATH+"/aging_log.txt");
    QObject::connect(&a, &QCoreApplication::aboutToQuit, []() {
            Logger::finalize();
        });
    MainWindow w;
    w.show();
    if (argc > 1 && QString(argv[1]) == "reboot"){
        qDebug()<<QString(argv[1]);
        w.enterRebootMode();
    }else if(argc > 1 && QString(argv[1]) == "booked"){
        w.enterBookedMode();
    }
    return a.exec();
}
